﻿/*****
 *  Copyright 2011, Kaven Gagné
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

using System;
using Knowledge.DataModel;
using System.Collections.ObjectModel;

namespace Knowledge
{
    public class FolderViewModel : ViewModelBase
    {
        #region Constructor
        public FolderViewModel(kFolder folder, ViewModelBase parent) : base(parent, true)
        {
            _folder = folder;
        } 
        #endregion //Constructor

        #region Data
        private kFolder _folder;
        #endregion //Data




        #region Presentation Members

        #region FolderID
        public int FolderID
        {
            get
            {
                return _folder.FolderID;
            }
        } 
        #endregion //FolderID
        
        #region FolderName
        public String FolderName
        {
            get
            {
                return _folder.FolderName;
            }
            set
            {
                _folder.FolderName = value;
                this.OnPropertyChanged("FolderName");
            }
        } 
        #endregion //FolderName

        #region Description
        public String Description
        {
            get
            {
                return _folder.Description;
            }
            set
            {
                _folder.Description = value;
                OnPropertyChanged("Description");
            }
        } 
        #endregion //Description

        #region Links
        public ObservableCollection<LinkViewModel> Links
        {
            get { return null; }
        }
        #endregion //Links

        #region Files
        public ObservableCollection<FileViewModel> Files
        {
            get { return null; }
        }
        #endregion //Files

        #endregion //Presentation Members




        #region Public Methods

        #region AddArticle
        public void AddArticle(string title)
        {
            if (Database.AddArticle(title, this.FolderID) >= 0)
            {
               // this.LoadArticles();
            }
        }
        #endregion //AddArticle

        #region AddFolder
        public void AddFolder(string title)
        {
            if (Database.AddFolder(title, this.FolderID) >= 0)
            {
               // this.LoadArticles();
            }
        }
        #endregion //AddFolder

        #region LoadDescription
        public int LoadDescription()
        {
            int sts = 0;
            string desc = Database.GetDescription(_folder, _folder.FolderID);
            if (desc == null)
            {
                // No description found, item might not exists
                sts = (int)Utilities.errCode.NO_ROW_AFFECTED;
                // Reload this folder in TreeView
                if (this.Parent is FolderViewModel)
                    ((FolderViewModel)this.Parent).LoadArticles();
                else
                    ((RootViewModel)this.Parent).LoadFolders();
            }
            else
            {
                this.Description = desc;
            }
            return sts;
            
        }
        #endregion //LoadDescription

        #region SaveDescription
        public int SaveDescription()
        {
            int sts = Database.SetDescription(_folder, _folder.FolderID, this.Description);
            if (sts == (int)Utilities.errCode.NO_ROW_AFFECTED)
            {
                // Reload this folder in TreeView
                if (this.Parent is FolderViewModel)
                    ((FolderViewModel)this.Parent).LoadArticles();
                else
                    ((RootViewModel)this.Parent).LoadFolders();
            }
            return sts;
        }
        #endregion //SaveDescription

        #endregion //Public Methods




        #region Private Methods
        
        #region LoadArticles
        public void LoadArticles()
        {
            base.Children.Clear();
            foreach (kFolder folder in Database.GetFolders(_folder))
                base.Children.Add(new FolderViewModel(folder, this));
            foreach (kArticle article in Database.GetArticles(_folder))
                base.Children.Add(new ArticleViewModel(article, this));
        }
        #endregion //LoadArticles
        
        #endregion //Private Methods




        #region Overrides
        protected override void LoadChildren()
        {
            this.LoadArticles();
        } 
        #endregion //Overrides
    }


    public class FolderViewModels : ObservableCollection<FolderViewModel> { }
}
